﻿$(function() {
	/**
	 * 电度电费
	 * */
    var complexpie1 = echarts.init(document.getElementById('complexpie1'));
    optionpie1 = {
    		tooltip: {
		        trigger: 'item',
		        formatter: "{a} <br/>{b}: {c} ({d}%)"
		    },
		    backgroundColor:'#e0e0e0',
		    legend: {
		        orient: 'vertical',
		        x: 'right',
		        data:[],
		    },
		    series: [{
	            name:'电费',
	            type:'pie',
	            radius: [0, '40%'],

	            label: {
	                normal: {
	                    position: 'inside'
	                }
	            },
	            labelLine: {
	                normal: {
	                    show: false
	                }
	            },
	            data:[]
	        },{
	            name:'电度',
	            type:'pie',
	            radius: ['55%', '80%'],
	            data:[]
	        }]
    };
    complexpie1.setOption(optionpie1);
    
    /**
     * 每日电度情况'
     * */
    var complexbar = echarts.init(document.getElementById('complexbar'));
    optionBar = {
    		tooltip : {
    	        trigger: 'axis',
    	        axisPointer : {            
    	            type : 'shadow'        
    	        }
    	    },
    	    grid: {
	            top: '10%',
	            left: '4%',
	            right: '4%',
	            bottom:'20%',
	            containLabel: true
	        },
    	    legend: {
    	    	bottom: '25',
    	        data:[],
    	    },
    	    dataZoom:{
    	    	type:'slider',
    	    	start:0,
    	    	end:100,
    	    	labelFormatter: function (value) {
    	    	    return value; // 返回最终的label内容。
    	    	},
    	    	bottom:1
    	    },
    	    backgroundColor:'#e0e0e0',
    	    xAxis:  {
            	type: 'category',
    	        data: [],
    	        splitLine:{
	            	show:false
	            }
    	    },
    	    yAxis: {
    	    	 type: 'value',
    	    	 name:'每日电度',
    	    	 axisLabel:{
    	    		 formatter: '{value}'
    	    	 },
    	    	 splitLine:{
 	            	show:false
 	            }
    	    },
    	    series: []
	};
    complexbar.setOption(optionBar);
    
    /**
     * 班次耗电情况
     * */
    var complexpie2 = echarts.init(document.getElementById('complexpie2'));
    optionpie2 = {
	        tooltip : {
	            trigger: 'item',
	            formatter: function (params, ticket, callback) {
	                if(params.seriesName=="班次电费"){
	                	return params.seriesName+" <br/>"+params.name+" : "+params.value+"分钟 "+"("+params.percent+"%)";
	                }else{
	                	return params.seriesName+" <br/>"+params.name+" : "+params.value+"度 "+"("+params.percent+"%)";
	                }
	            }
	        },
	        legend: {
	        	orient:'vertical',
	        	left:'center',
	        	top:'middle',
	            data: []
	        },
	        backgroundColor:'#e0e0e0',
	        series : [{
	        	name:'班次耗电',
	            type:'pie',
	            radius:'60%',
	            center:['25%', '45%'],
	            data:[],
	            label:{
	                normal: {
	                	show:false,
		                 formatter: '{b}\n{c}度'
		            }
	            }
	        },{
	        	name:'班次电费',
	            type: 'pie',
	            radius : '60%',
	            center : ['75%', '45%'],
	            data:[],
	            label:{
	                normal: {
	                	show:false,
		                 formatter: '{b}\n{c}元'
		            }
	            }
	        }]
	    };
    complexpie2.setOption(optionpie2);
    

    
    var lineId=0;
    var	startTime="";
    var	endTime="";
    
    /**
     * slider滑动事件
     * */
    $('#ss').slider({
    	range: true,
        onComplete:function(value){//滑块值改变时触发
	       	startTime=moment(value[0]).format('YYYY-MM-DD');
	       	endTime=moment(value[1]).format('YYYY-MM-DD');
	       	getData();
       }
    });
    /**
     * 初始化
     * */
    var vm = new Vue({
	    el: '#itemData',
	    data:{item:[]}
	}); 

    var timess=[parseInt(moment().subtract(7, 'days').format("x")),parseInt(moment().format("x"))];
    $('#ss').slider('setValues',timess);
    var arr=$('#ss').slider('getValues');
    var time1=arr[0];
    var time2=arr[1];
    startTime=moment(time1).format("YYYY-MM-DD");
    endTime=moment(time2).format("YYYY-MM-DD");
    getData();
    function getData(){
    	$.post(basePath+"/line/lineComplex/electricity.json",{"lineId":lineId,startTime:startTime,endTime:endTime},function(res){
    		vm.$set('item',res);
    		if(res.success){
    			complexpie1.setOption({
    				legend:{
    					data:res.legType
    				},
    				series:[{
    					name:'电费',
    					data:res.chargePie
    				},{
    					name:'电度',
    					data:res.electricityPie
    				}]
    			});
    			var series=[];
    		    for(var i = 0;i<res.electricityBar.length;i++){
    		        series.push({
    		            name: res.electricityBar[i].name,
    		            stack: '总量',
    		            type: 'bar',
    		            barMaxWidth:40,
    		            data: res.electricityBar[i].value
    		        });
    		    }
    		    complexbar.setOption({
    				legend: {
    		            data: res.legBar
    		        },
    				xAxis: {
			            data: res.everyday
			        },
			        series:series
    			});
    		    complexpie2.setOption({
    				legend: {
    		            data: res.shiftNames
    		        },
    		        series: [{
    		        	name:'班次耗电',
			            data: res.groupElectricityPie
			        },{
    		        	name:'班次电费',
			            data: res.groupChargePie
			        }]
    			});
    		}else{
    			alert("出现跨班制或者无班次情况，请重新选择时间区间");
    		}
    	});
    }
    
    /**
     * 自定义时间按钮的点击事件
     * */
	 $("#defineTime").click(function (){
		 var mePanel = $('#defineTimeWin');
	   	 mePanel.dialog('open');
	 });
	 
	 /**
	  * 切换设备的点击事件
	  * */
	var mePanel = $('#switchEquip');
	$("#linetree").tree({
		onDblClick: function(node) {
			if (node.url) {
				if(node.shifts){
					lineId=node.id;
					var arr=$('#ss').slider('getValues');
		 		    var time1=arr[0];
		 		    var time2=arr[1];
		 		    startTime=moment(time1).format("YYYY-MM-DD");
		 		    endTime=moment(time2).format("YYYY-MM-DD");
		 		    getData();
		 		    mePanel.dialog('close');
				}else{
					$.messager.alert({showSpeed:100,title:'消息提示',msg:'该生产线无班次，请重新选择',timeout:3000});
				}
			}else{
				$.messager.alert({showSpeed:100,title:'消息提示',msg:'请选择生产线！',timeout:3000});
			}
		}
	});
   $("#switchEquipOK").click(function(){
   	var row = $("#linetree").tree('getSelected');
   	if(row.url){
   		if(row.shifts){
			lineId=node.id;
			var arr=$('#ss').slider('getValues');
 		    var time1=arr[0];
 		    var time2=arr[1];
 		    startTime=moment(time1).format("YYYY-MM-DD");
 		    endTime=moment(time2).format("YYYY-MM-DD");
 		    getData();
 		    $('#switchEquip').dialog('close');
		}else{
			$.messager.alert({showSpeed:100,title:'消息提示',msg:'该生产线无班次，请重新选择',timeout:3000});
		}
   	}else{
		$.messager.alert({showSpeed:100,title:'消息提示',msg:'请选择生产线！',timeout:3000});
	}
   });

});